/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.provider;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;

import com.xuexiang.xuidemo.ResourceTable;

import java.util.List;

/**
 * ViewPager Item 适配器
 *
 * @since 2021-04-22
 */
public class ViewPagerItemSelectProvider extends BaseItemProvider {
    private List<String> itemString;
    private OnItemClickListener onItemClickListener; // 接口回调

    /**
     * 传参构造
     *
     * @param stringList
     */
    public ViewPagerItemSelectProvider(List<String> stringList) {
        itemString = stringList;
    }

    @Override
    public int getCount() {
        return itemString.size();
    }

    @Override
    public String getItem(int position) {
        return itemString.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        ViewHolder viewHolder = null;
        Component component1 = component;
        if (null == component1) {
            LayoutScatter instance = LayoutScatter.getInstance(componentContainer.getContext());
            component1 = instance.parse(ResourceTable.Layout_item_select_adapter, null, false);
            viewHolder = new ViewHolder(component1);
            componentContainer.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) componentContainer.getTag();
        }
        viewHolder.itemText.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (onItemClickListener != null) {
                    onItemClickListener.setItemChange(position);
                }
            }
        });
        viewHolder.itemText.setText(itemString.get(position));
        return component1;
    }

    /**
     * ViewHolder
     *
     * @since 2021-04-22
     */
    public static class ViewHolder {
        private Text itemText;

        public ViewHolder(Component component) {
            itemText = (Text) component.findComponentById(ResourceTable.Id_itemText);
        }
    }

    /**
     * 条目点击监听
     *
     * @since 2021-04-20
     */
    public interface OnItemClickListener {
        /**
         * 条目索引
         *
         * @param position
         */
        void setItemChange(int position);
    }

    public void setOnItemClick(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }
}
